﻿/*
* 学生管理
*/
function StudentIndex() {
    this.urlRoot = ctx + '/Student/';
    this.tableObj;

}

StudentIndex.prototype.initTable = function () {
    var This = this;

    var h = $(window).height() - 5;
    this.tableObj = $('#listTable').bootstrapTable({
        url: This.urlRoot + 'list',
        height: h,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        toolbar: '#tableTool',
        queryParams: function (params) {
            var pageIndex = params.offset / params.limit + 1

            $('#page').val(pageIndex);
            $('#rows').val(params.limit);
            var searchData = $('#searchForm').serializeJson();
            return searchData;
        },
        columns: [
            {
                field: 'id',//第一列序号
                title: '操作',
                width: '100px',
                align: 'center',
                formatter: function (value, row, index) {
                    var html = '<button class="btn btn-info btn-circle btn-outline" title="编辑" onclick="StudentIndex.dialog(true,' + value + ');"><span class="glyphicon glyphicon-pencil"></span></button>';
                    html = html + '&nbsp;<button class="btn btn-danger btn-circle btn-outline" title="删除" onclick="StudentIndex.del(' + value + ');"><span class="glyphicon glyphicon-trash"></span></button>';
                    return html;
                }
            },
            {
                field: 'schoolName',
                title: '所属学校',
                align: 'center'
            },
            {
                field: 'className',
                title: '所属班级',
                align: 'center'
            },
            {
                field: 'teacherName',
                title: '班主任',
                align: 'center'
            },
            {
                field: 'studentName',
                title: '姓名',
                align: 'center'
            },
            {
                field: 'sex',
                title: '性别',
                align: 'center',
                formatter: function (v, r, i) {
                    if (v) {
                        return '男';
                    }
                    return '女';
                }
            },
            {
                field: 'state',
                title: '状态',
                align: 'center',
                formatter: function (v,r,i) {
                    if (v == 1) {
                        return '启用';
                    }
                    if (v == 0) {
                        return '停用';
                    }
                    return '无效状态：'+v;
                }
            }, {
                field: 'updateTime',
                title: '最后修改时间',
                align: 'center'
            }, {
                field: 'updateUser',
                title: '最后修改人',
                align: 'center'
            }, {
                field: 'createTime',
                title: '创建时间',
                align: 'center'
            }, {
                field: 'createUser',
                title: '创建人',
                align: 'center'
            }
        ]
    });
}

/*
打开编辑窗口，是否更新，更新加载的数据
*/
StudentIndex.dialog = function (isUpdate, id) {
    var title = '添加学生';
    var url = '../Student/dialog';
    if (isUpdate) {
        title = "修改学生信息";
        url = '../Student/dialog?id=' + id;
    }

    window.parent.layer.open({
        title: title,
        type: 2,
        content: url,
        scroll: true,
        btn: [],
        area: ['800px', '600px'],
        cancel: function () {
            $('#listTable').bootstrapTable('refresh');
        }
    });
}

StudentIndex.prototype.getSelectRow = function () {
    var selectRows = this.tableObj.bootstrapTable('getSelections');
    if (selectRows.length == 0) {
        layer.alert('请选择一行数据', { icon: 6 });
        return null;
    }
    return selectRows[0];
}

StudentIndex.prototype.reloadTable = function () {
    this.tableObj.bootstrapTable('refresh');
}

StudentIndex.prototype.bind = function () {
    var This = this;

    $('#add').click(function () {
        StudentIndex.dialog(false, null);
    });

    $('#import').click(function () {
        var schoolName = $('#schoolName').val();
        var classId = $('#classId').val();
        if (classId == "" || schoolName == "") {
            layer.alert('导入需要先选择学校和班级', { icon: 6 });
            return;
        }
        $('#fileForm')[0].reset();
        var className = $('#classId option:selected').text();

        $('#imp_schoolName').val(schoolName);
        $('#imp_classId').val(classId);

        layer.open({
            title: '导入：' + schoolName + ',' + className+' 的学生数据',
            type: 1,
            content: $('#uploadExcelContent'),
            btn: [],
            area: ['500px', '240px']
        });
    });
    $('#uploadExcel').click(function () {
        var fileSize = $("#excelFile")[0].files;
        if (fileSize.length == 0) {
            layer.alert('请选择文件', { icon: 5 });
            return;
        }
        $('#importInfo').empty();
        $('#fileForm').ajaxSubmit({
            url: This.urlRoot + 'importStudent',
            type: 'post',
            dataType: 'json',
            beforeSubmit: function () {
                custom.loadTitle('正在上传中...');
            },
            success: function (serverData) {
                custom.closeTitle();
                if (serverData.Statu == 0) {
                    if (serverData.Data.length == 0) {
                        layer.msg('导入成功', { icon: 1 });
                        $('#listTable').bootstrapTable('refresh');
                    } else {
                        layer.open({
                            title: '导入信息',
                            type: 1,
                            content: $('#importInfo'),
                            btn: [],
                            area: ['670px', '92%'],
                            success: function () {
                                if (serverData.Data.length > 0) {
                                    $('#importInfo').empty();
                                    for (var i = 0; i < serverData.Data.length; i++) {
                                        $('#importInfo').append($('<p>' + serverData.Data[i] + '</p>'));
                                    }
                                }
                                layer.msg('导入失败', { icon: 5 });
                            }
                        });
                    }
                } else {
                    layer.alert('导入失败:' + serverData.Msg, { icon: 5 });
                }
            },
            error: function () {
                layer.close(loadTitleIndex);
                layer.alert('导入失败', { icon: 5 });
            }
        });
    });


    $('#search').click(function () {
        This.reloadTable();
    });
    $('#resetForm').click(function () {
        $('#searchForm')[0].reset();
    });

    $('#schoolName').change(function () {
        var unit = $(this).val();
        $.post(ctx + '/Student/getSchoolClass', { school: unit }, function (server) {
            if (server.Statu == 0) {
                $('#classId').empty();
                for (var i in server.Data) {
                    $('#classId').append($('<option value="' + server.Data[i].value + '">' + server.Data[i].text + '</option>'));
                }
            } else {
                layer.alert('加载班级列表失败', { icon: 5 });
            }
        });
    });
}

StudentIndex.del = function (id) {
    var del = function () {
        var loadTitleIndex = layer.msg('删除中...', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
        $.post(ctx + '/Student/del', { id: id }, function (server) {
            layer.close(loadTitleIndex);
            if (server.Statu == 0) {
                $('#listTable').bootstrapTable('refresh');
                layer.msg('删除成功', { icon: 1 });
            } else {
                layer.alert('删除失败', { icon: 5 });
            }
        });
    }

    layer.open({
        content: '确认删除吗？',
        btn: ['删除', '取消'],
        icon: 2,
        yes: function (index, layero) {
            del();
        }
    });
}

$(function () {
    $('.files').prettyFile();
    var obj = new StudentIndex();
    obj.bind();
    obj.initTable();
});